using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LevelingScript : MonoBehaviour
{
    public Slider roleSlider;//主角血条
    public Slider enemySlider;//敌人血条
    //人物满血数值=等级*10+魅力值+外貌+情缘+好感
    int roleFullBlood = VariableDocument.GetInstance.Grade() * 10 + VariableDocument.GetInstance.attributes[0] + VariableDocument.GetInstance.attributes[1] + VariableDocument.GetInstance.attributes[2] + VariableDocument.GetInstance.attributes[3];
    //敌人满血数值=敌人失败次数*10+50
    int enemyFullBlood = VariableDocument.GetInstance.enemyFailNum * 30+100;
    float roleBlood;//主角血量
    float enemyBlood;//敌人血量
    float roleAttack;//主角攻击力
    float enemyAttack;//敌人攻击力
    public Animator roleAni;//主角动画
    public GameObject knifeLightAni;//刀光动画
    public Animator enemyAni;//敌人动画

    //结算
    public GameObject endPanel;
    public Image bg;//背景
    public Image Back1;//返回
    public Image AttributeIncrease1;//奖励1图
    public Image AttributeIncrease2;//奖励2图
    public Text AttributeAddValue1;//奖励1值
    public Text AttributeAddValue2;//奖励2值

    //等级
    public Text roleGrade;
    public Text enemyGrade;

    private void Awake()
    {
        roleGrade.text = "洛夜  LV：" + VariableDocument.GetInstance.Grade();
        enemyGrade.text = "敌人  LV：" + VariableDocument.GetInstance.enemyFailNum;
        //roleAni.SetBool("LeftBattle",true);
        Random.InitState((int)System.DateTime.Now.Ticks);//防止伪随机数
        //主角初始满血
        roleSlider.value = 1;
        roleBlood = roleFullBlood;
        enemySlider.value = 1;
        enemyBlood = enemyFullBlood;//敌人初始满血
        roleAttack = (int)(roleFullBlood * Random.Range(10, 20) * 0.01f);
        enemyAttack = (int)(enemyFullBlood * Random.Range(10, 20) * 0.01f);
    }
    // Start is called before the first frame update   ：：：
    void Start()
    {
        RoleBattle();
    }
    //主角进行攻击
    void RoleBattle()
    {
        
        //Debug.Log("enemyBlood---" + enemyBlood);
        //Debug.Log("roleAttack---"+roleAttack);
        
        roleAni.SetBool("LeftBattle",true);
        //Debug.Log("roleAni is " +roleAni.GetBool("LeftBattle"));
        knifeLightAni.SetActive(true);
        if (enemyBlood > 0)
        {
            //敌人失血
            enemyBlood -= roleAttack;
            //设定下次主角攻击力
            roleAttack = roleFullBlood * Random.Range(10, 20) * 0.01f;
            //2秒后停止主角攻击动画
            Invoke("Pause1",2f);  
        }
    }
    void Pause1()
    {
        //滑动条变化
        enemySlider.value = enemyBlood / enemyFullBlood;
        //停止主角攻击状态，改为休闲状态
        roleAni.SetBool("LeftBattle", false);
        //如果敌人没死
        if (enemyBlood > 0)
        {
            //敌人进行攻击
            Invoke("EnemyBattle", 1f);
        }
        else//1秒后否则弹出结算页面
        {
            Invoke("VactoryFindEndPanel",1f);
        }
    }
    void Pause2()
    {
        roleSlider.value = roleBlood / roleFullBlood;
        enemyAni.SetBool("RightBattle", false);
        if (roleBlood > 0)
        {
            Invoke("RoleBattle", 1f);
        }
        else
        {

            //FindEndPanel();
            Invoke("FailFindEndPanel", 1f);
            
        }
    }
    //敌人进行攻击
     void EnemyBattle()
    {
        
        //Debug.Log("roleBlood---" + roleBlood);
        //Debug.Log("enemyAttack---" + enemyAttack);
        
        knifeLightAni.SetActive(false);
        enemyAni.SetBool("RightBattle", true);
        
        if (roleBlood > 0)
        {
            roleBlood -= enemyAttack;
            enemyAttack = enemyFullBlood * Random.Range(10, 20) * 0.01f;
            Invoke("Pause2", 2f);
            
        }

    }
    //胜利
     void VactoryFindEndPanel()
    {
        Debug.Log("VactoryFindEndPanel");
        enemySlider.value = 0;
        enemyAni.gameObject.SetActive(false);
        roleAni.SetBool("LeftBattle", false);
        knifeLightAni.SetActive(false);
        enemyAni.SetBool("RightBattle", false);
        endPanel.SetActive(true);
        int a = Random.Range(0,4);
        int b = Random.Range(0, 4);
        //魅力//外貌//情缘//好感
        string[] c = { "Leveling/魅力+", "Leveling/容貌+", "Leveling/情缘+", "Leveling/好感+" };
        while (a == b)
        {
            b = Random.Range(0, 4);
        }
        int d = Random.Range(30, 50);
        int e = Random.Range(30, 50);
        bg.sprite = Resources.Load<Sprite>("Leveling/组-37");
        Back1.sprite = Resources.Load<Sprite>("Leveling/组-36");
        AttributeIncrease1.sprite = Resources.Load<Sprite>(c[a]);
        AttributeIncrease2.sprite = Resources.Load<Sprite>(c[b]);
        AttributeAddValue1.text = "" + d;
        AttributeAddValue2.text = "" + e;
        VariableDocument.GetInstance.attributes[a] += d;
        VariableDocument.GetInstance.attributes[b] += e;
    }
    //失败
    void FailFindEndPanel()
    {
        Debug.Log("FailFindEndPanel");
        roleSlider.value = 0;
        roleAni.gameObject.SetActive(false);
        roleAni.SetBool("LeftBattle", false);
        knifeLightAni.SetActive(false);
        enemyAni.SetBool("RightBattle", false);
        endPanel.SetActive(true);
        bg.sprite = Resources.Load<Sprite>("Leveling/组-35");
        Back1.sprite = Resources.Load<Sprite>("Leveling/色相_饱和度-32");
        AttributeIncrease1.gameObject.SetActive(false);
        AttributeIncrease2.gameObject.SetActive(false);
    }
}
